package com.zhaoqi.hr.controller;

import com.zhaoqi.hr.dto.request.DocumentAuditRequest;
import com.zhaoqi.hr.dto.request.DocumentUploadRequest;
import com.zhaoqi.hr.dto.response.ApiResponse;
import com.zhaoqi.hr.dto.response.DocumentResponse;
import com.zhaoqi.hr.security.CurrentUser;
import com.zhaoqi.hr.security.UserPrincipal;
import com.zhaoqi.hr.service.DocumentService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 文档管理控制器
 */
@RestController
@RequestMapping("/documents")
public class DocumentController {

    private static final Logger log = LoggerFactory.getLogger(DocumentController.class);

    private final DocumentService documentService;

    public DocumentController(DocumentService documentService) {
        this.documentService = documentService;
    }

    /**
     * 上传文档
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResponse<DocumentResponse> uploadDocument(
            @CurrentUser UserPrincipal currentUser,
            @Valid @ModelAttribute DocumentUploadRequest request,
            @RequestParam("file") MultipartFile file) {
        
        DocumentResponse response = documentService.uploadDocument(
                currentUser.getId(), request, file);
        return ApiResponse.success(response);
    }

    /**
     * 获取当前用户的文档列表
     */
    @GetMapping("/my")
    public ApiResponse<List<DocumentResponse>> getMyDocuments(
            @CurrentUser UserPrincipal currentUser) {
        
        List<DocumentResponse> documents = documentService.getUserDocuments(currentUser.getId());
        return ApiResponse.success(documents);
    }

    /**
     * 获取文档详情
     */
    @GetMapping("/{documentId}")
    public ApiResponse<DocumentResponse> getDocument(
            @PathVariable Long documentId) {
        
        DocumentResponse document = documentService.getDocumentById(documentId);
        return ApiResponse.success(document);
    }

    /**
     * 删除文档
     */
    @DeleteMapping("/{documentId}")
    public ApiResponse<Void> deleteDocument(
            @CurrentUser UserPrincipal currentUser,
            @PathVariable Long documentId) {
        
        documentService.deleteDocument(currentUser.getId(), documentId);
        return ApiResponse.success(null);
    }

    /**
     * 获取待审核文档列表（管理员专用）
     */
    @GetMapping("/admin/pending")
    public ApiResponse<Page<DocumentResponse>> getPendingDocuments(Pageable pageable) {
        Page<DocumentResponse> documents = documentService.getPendingDocuments(pageable);
        return ApiResponse.success(documents);
    }

    /**
     * 审核文档（管理员专用）
     */
    @PostMapping("/admin/audit")
    public ApiResponse<DocumentResponse> auditDocument(
            @CurrentUser UserPrincipal currentUser,
            @Valid @RequestBody DocumentAuditRequest request) {
        
        DocumentResponse response = documentService.auditDocument(
                currentUser.getId(), request);
        return ApiResponse.success(response);
    }
}
